<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>小罗后台管理系统</title>
  <link rel="stylesheet" href="/assets/layui/css/layui.css">
</head>

<body>
  <div class="layui-layout layui-layout-admin">
    <!-- 导入公共页面 -->
    {{ include "./common/header.html" }}

    <!-- 导入公共页面 -->
    {{ include "./common/side.html" }}
    <div class="layui-body">
      <!-- 内容主体区域 -->
      <div style="padding: 15px;">
        <span class="layui-breadcrumb" lay-separator="/">
          <a href="/">首页</a>
          <a href="/category">分类列表</a>
        </span>
        <br>
        <button type="button" class="layui-btn layui-btn-normal"><a href="/addCategory">分类添加</a></button>
        <table class="layui-table" id="cate" lay-filter="cate">
        </table>
      </div>
    </div>

  </div>
</body>

<script src="/assets/layui/layui.js"></script>
<script>
  layui.use('table', function () {
    var table = layui.table;
    var $ = layui.jquery;
    table.render({
      elem: '#cate'
      , url: '/cateData'
      , cols: [[
        { field: 'cate_id',  title: '序号', sort: true }
        , { field: 'cate_name',edit: 'text', title: '分类名称' }
        , { field: 'orderBy', edit: 'text', title: '排序', sort: true }
        , {
          fixed: "right",title: '操作', templet: (obj) => {
            return `
          <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
          `
          }
        }
      ]]
    });
    // 编辑
    table.on('edit(cate)', async function (obj) {
      const { data } = obj;
      const { code, message } = await $.post('/updCateData', data)
      if (code === 0) {
        layer.msg(message)
      }
    });
    //删除 
    table.on('tool(cate)', function (obj) {
      const { event, data } = obj;
      const { cate_id } = data;
      if (event === 'del') {
        layer.confirm('确认删除吗', {
          btn: ['是的', '取消']
        }, async function () {
          const { code } = await $.post('/deleteCate', { cate_id })
          if (code === 0) {
            layer.msg('删除成功', { icon: 1 });
            table.reload('cate')
          }

        }, function () {

        });
      }


    });
  });
</script>

</html>